﻿using DFGK.IService.BBS;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.BBS;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.Platform
{
    /// <summary>
    /// 公告类别管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BBS_AreaController : BaseController
    {
        private readonly IBBS_AreaService _bbs_AreaService;

        public BBS_AreaController(IBBS_AreaService bbs_AreaService, IWebHostEnvironment webEnvironment) : base(webEnvironment)
        {
            _bbs_AreaService = bbs_AreaService;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="areaName">类别名称</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPageData(string? areaName, int pageIndex, int pageSize)
        {
            int total = 0;
            var page = _bbs_AreaService.GetPageData(areaName, out total, pageIndex, pageSize);
            return Ok(BaseResult.Result(page, "查询成功！", "200", total));
        }

        /// <summary>
        /// 删除公告类别
        /// </summary>
        /// <param name="areaId">类别id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelArea(long areaId)
        {
            var result = _bbs_AreaService.DelArea(areaId, out string errorMsg);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！" + errorMsg, "500"));
        }

        /// <summary>
        /// 新增/修改公告类别
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SubmitArea(BBS_Area model)
        {
            if (model.Id > 0)
            {
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
            }
            else
            {
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateUserName = base.BaseGetNowUserName();
            }
            bool result = _bbs_AreaService.SubmitArea(model, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// Id查询
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetArea(long areaId)
        {
            var cobj = _bbs_AreaService.GetArea(areaId);
            if (cobj != null)
            {
                return Ok(BaseResult.Result(cobj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
